# 作者: 陈其志 
# 2025年02月24日19时01分28秒 
# 2716847110@qq.com
from operator import itemgetter
from operator import attrgetter

# list_1 = [1, 3, 2, 4, 5, 3, 2, 1]
# dict_1 = {1: 'D', 2: 'B', 3: 'B', 4: 'E', 5: 'A'}
#
# print(sorted(list_1))
# print(list_1)
# list_1 = [1, 3, 2, 4, 5, 3, 2, 1]
# list_1.sort()
# print(list_1)
#
# # [1, 2, 3, 4, 5]对键进行排序
# print(sorted({1: 'D', 2: 'B', 3: 'B', 4: 'E', 5: 'A'}))
# str_list = 'china Is string test first but japan in This shit'.split()
# print(str_list)
# # str_list.sort()
# # print(str_list)
#
# print(sorted(str_list))
# print(str_list)
#
#
# def str_lower(str1: str):
#     return str1.lower()
#
#
# # str是
# print(sorted(str_list, key=str_lower))
# print(sorted(str_list, reverse=True))
#
# # 列表嵌套元组：
#
student_tuples = [
    ('john', 'A', 15),
    ('jane', 'B', 12),
    ('dave', 'B', 10), ]


# print(sorted(student_tuples))  # 默认按照第一个元素排序
# # 匿名函数：lambda x: x[2]  # 函数在当前位置只使用一次，不需要在其他地方使用
# print(sorted(student_tuples, key=lambda x: x[2]))
# for i in student_tuples:
#     print(i)
#     print(i[2])


class Student:
    def __init__(self, name, grade, age):
        self.name = name
        self.grade = grade
        self.age = age

    def __repr__(self):  # 打印对象时显示的内容不能和__str__重复
        """
        类似__str__，但返回值是字符串，__repr__返回值可以是元组、列表、字典等任意可打印的对象
        :return:
        """
        return repr((self.name, self.grade, self.age))

    def __str__(self):  # 打印对象时显示的内容
        """
        一半str和repr不同时使用
        :return:
        """
        return '__str__'


student_objects = [
    Student('john', 'A', 15),
    Student('jane', 'B', 12),
    Student('dave', 'B', 10)
]
# 感受排序的稳定性：
# 1. 相同的元素，排序前后顺序不变
# 2. 不同元素，排序前后顺序可能不同
data = [('red', 1), ('blue', 2), ('red', 2), ('blue', 1)]
# lambda student: student.age这里的lambda student: 是把student作为参数传入到函数中，而不是把student_objects作为参数传入到函数中
mydict = {'Li': ['M', 7],
          'Zhang': ['E', 2],
          'Wang': ['P', 3],
          'Du': ['C', 2],
          'Ma': ['C', 9],
          'Zhe': ['H', 7]}

game_result = [
    {"name": "Bob", "wins": 10, "losses": 3, "rating": 75.00},
    {"name": "David", "wins": 3, "losses": 5, "rating": 57.00},
    {"name": "Carol", "wins": 4, "losses": 5, "rating": 57.00},
    {"name": "Patty", "wins": 9, "losses": 3, "rating": 71.48}]

if __name__ == '__main__':
    print(sorted(student_objects, key=lambda student: student.name))
    s = Student('john', 'A', 15)
    print(s)
    print(sorted(student_tuples, key=itemgetter(2)))  # 用于元组或列表
    print(sorted(student_objects, key=attrgetter('age')))  # 用于对象
    print('-' * 50)
    print(sorted(student_tuples, key=itemgetter(1, 2)))  # 用于元组或列表
    print(sorted(student_objects, key=attrgetter('grade', 'age')))  # 用于对象
    print('-' * 50)
    print(sorted(student_tuples, key=lambda x: x[1:], reverse=True))  # x[1:]表示取元组的第二个元素到最后一个元素
    print(sorted(student_objects, key=lambda student: (student.grade, student.age), reverse=True))  # lambda函数中可以调用多个属性,
    print('-' * 50)
    print(sorted(data, key=itemgetter(0)))
    print('-' * 50)
    for i in mydict.items():
        print(i)
    print(sorted(mydict.items(), key=lambda v: v[1][1]))
    print('-' * 50)
    for i in game_result:
        print(i)
    # print(sorted(game_result, key=lambda result:result.items('rating') ))
    print(sorted(game_result, key=itemgetter('rating', 'name')))
